package com.wu.web;

import com.wu.bean.User;
import com.wu.service.UserService;
import com.wu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录
 */
@WebServlet("/Login")
public class SelectLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        UserService userService=new UserServiceImpl();
        //登录成功  session缓存
        User user=userService.Login(new User(null,username,null,password,null,null,null,null,null));

        if(user!=null)
        {
            HttpSession session=req.getSession();
            session.setAttribute("username",username);
            session.setAttribute("userid",user.getId());
            if("wu123".equals(username)){
                //resp.getWriter().print("admin");
              //  req.getRequestDispatcher("/blog.html").forward(req,resp);
                resp.getWriter().print("ok");
               // resp.sendRedirect(req.getContextPath()+"/blog.html");

            }else
            {
                resp.getWriter().print(username);
//                req.getRequestDispatcher("/index.html").forward(req,resp);
            }

        }else{
            resp.getWriter().print("no");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
